package com.open.capacity.reimburse.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.StdTrafficAllowance;
import com.open.capacity.reimburse.service.StdTrafficAllowanceService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 市内交通补贴
 *
 * @author
 * @email
 * @date 2021-03-19 11:50:53
 */
@RestController
@RequestMapping("stdtrafficAllowances")
@Api(tags = "市内交通补贴")
public class StdTrafficAllowanceController {

    @Autowired
    private StdTrafficAllowanceService stdTrafficAllowanceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('std_traffic_allowance:stdtrafficallowance:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = stdTrafficAllowanceService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('std_traffic_allowance:stdtrafficallowance:list')")
    public Result findById(String id) {
        return Result.succeed(stdTrafficAllowanceService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('std_traffic_allowance:stdtrafficallowance:save')")
    @LogAnnotation(module = "市内交通补贴", name = "保存")
    public Result save(@RequestBody @Valid StdTrafficAllowance stdTrafficAllowance, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            stdTrafficAllowanceService.save(stdTrafficAllowance);
        } else {
            stdTrafficAllowance.setId(keyValue);
            stdTrafficAllowanceService.update(stdTrafficAllowance);
        }
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('std_traffic_allowance:stdtrafficallowance:delete')")
    @LogAnnotation(module = "市内交通补贴", name = "删除")
    public Result delete(@PathVariable String id) {
        stdTrafficAllowanceService.delete(id);
        return Result.succeed("删除成功");
    }

    /**
     * 查询应补贴总额
     */
    @RequestMapping("/getMoney")
    public Result getMoney(String userId, String followUserids, Integer persons, String destination, @DateTimeFormat(pattern = "yyyy-MM-dd HH")Date startTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH")Date endTime) {
        BigDecimal data = stdTrafficAllowanceService.getMoney(userId, followUserids, persons, destination, startTime, endTime);
        return Result.succeed(data);
    }
}
